package com.sj.common.exception;

import com.sj.common.lang.ApiResultMsg;
import com.sj.common.lang.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUploadBase;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletResponse;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021-08-02
 * Time: 上午 10:13
 * Description: 全局异常处理
 *
 * @author xiaoqi
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = CustomException.class)
    public ApiResultMsg handler(CustomException e) {
        log.error("接口异常，{}", e.getMessage());
        return ApiResultMsg.error(e.getMessage());
    }


    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public ApiResultMsg handler(IllegalArgumentException e) {
        log.error("参数异常，{}", e.getMessage());
        return ApiResultMsg.error(e.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = MultipartException.class)
    public ApiResultMsg handler(MaxUploadSizeExceededException e) {
        log.error("文件上传异常，{}", e.getMessage());
        return ApiResultMsg.error("文件过大，最大上传2MB文件");
    }

    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(value = AccessDeniedException.class)
    public ApiResultMsg handler(AccessDeniedException e) {
        log.error("运行时异常，{}", e.getMessage());
        return ApiResultMsg.error(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public ApiResultMsg handlerNotFoundException(NoHandlerFoundException e) {
        log.error("资源未找到异常，{}", e.getMessage());
        return ApiResultMsg.error(ResultInfo.NOT_FONT);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ApiResultMsg handler(MethodArgumentNotValidException e) {
        log.error("方法参数异常，{}", e.getMessage());
        return ApiResultMsg.error(HttpServletResponse.SC_BAD_REQUEST, e.getBindingResult().getFieldError().getDefaultMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = DataAccessException.class)
    public ApiResultMsg handler(SQLIntegrityConstraintViolationException e) {
        log.error("sql异常，{}", e.getMessage());
        return ApiResultMsg.error(HttpServletResponse.SC_BAD_REQUEST, "请不要重复添加");
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RuntimeException.class)
    public ApiResultMsg handler(RuntimeException e) {
        log.error("运行时异常，{}", e.getMessage());
        return ApiResultMsg.error(e.getMessage());
    }

}